package com.winkyle.woodenfish.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.databinding.CustomDialogBinding;
import com.winkyle.woodenfish.mvpview.DialogView;

/**
 * @author winkyle
 * @date 2018/06/15
 * @description 自定义对话框实现类
 */

public class CustomDialog implements DialogView {

    private Dialog dialog;
    private CustomDialogBinding customDialogBinding;
    public CustomDialog(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null, false);
        customDialogBinding = DataBindingUtil.bind(view);
        customDialogBinding.setCustomDialogView(this);
        dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setContentView(view);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        WindowManager manager = ((Activity)context).getWindowManager();
        Display display = manager.getDefaultDisplay();
        layoutParams.width = display.getWidth()/3*2;
        view.setLayoutParams(layoutParams);
        dialog.getWindow().setGravity(Gravity.CENTER);
    }

    /**
     * @description 显示对话框
     * @return
     */
    public void show(){
        dialog.show();
    }

    /**
     * @description 隐藏对话框
     * @return
     */
    public void dismiss(){
        dialog.dismiss();
    }

    /**
     * @description 设置标题
     * @param titleText 标题文本
     * @return
     */
    public CustomDialog setTitleText(String titleText) {
        customDialogBinding.setTitle(titleText);
        return this;
    }

    /**
     * @description 设置内容
     * @param contentText 内容文本
     * @return
     */
    public CustomDialog setContentText(String contentText) {
        customDialogBinding.setContent(contentText);
        return this;
    }

    /**
     * @description 设置复选框内容
     * @param checkText 复选框内容文本
     * @return
     */
    public CustomDialog setCheckText(String checkText){
        customDialogBinding.setCheckText(checkText);
        return this;
    }

    /**
     * @description 设置确定按钮文本
     * @param confirmText 确定按钮文本
     * @return
     */
    public CustomDialog setConfirmText(String confirmText){
        customDialogBinding.setConfirm(confirmText);
        return this;
    }

    /**
     * @description 设置取消按钮文本
     * @param cancelText 取消按钮文本
     * @return
     */
    public CustomDialog setCancelText(String cancelText){
        customDialogBinding.setCancel(cancelText);
        return this;
    }

    @Override
    public void confirm() {
        if(confirmListener == null){
            dismiss();
            return;
        }
        confirmListener.onClick(this);
    }

    @Override
    public void cancel() {
        if(cancelListener == null) {
            dismiss();
            return;
        }
        cancelListener.onClick(this);
    }

    @Override
    public void check() {
        customDialogBinding.check.setChecked(!customDialogBinding.check.isChecked());
    }

    /**
     * @description 复选框是否选中
     * @return
     */
    public boolean isChecked(){
        return  customDialogBinding.check.isChecked();
    }

    private DialogClickListener confirmListener;
    private DialogClickListener cancelListener;


    /**
     * @description 设置确定按钮监听
     * @param confirmListener 确定按钮监听实例
     * @return
     */
    public CustomDialog setConfirmListener(DialogClickListener confirmListener) {
        this.confirmListener = confirmListener;
        return this;
    }

    /**
     * @description 设置取消按钮监听
     * @param cancelListener 取消按钮监听实例
     * @return
     */
    public CustomDialog setCancelListener(DialogClickListener cancelListener) {
        this.cancelListener = cancelListener;
        return this;
    }

    /**
     * @description 对话框点击监听
     */
    public interface DialogClickListener{
        /**
         * @description 对话框被点击调用
         * @param customDialog 对话框本身
         * @return
         */
        void onClick(CustomDialog customDialog);
    }
}
